load("//tools:defaults.bzl", "api_golden_test", "api_golden_test_npm_package", "generate_api_docs", "ng_package", "ng_project", "ts_config")

package(default_visibility = ["//visibility:public"])

ts_config(
    name = "tsconfig_build",
    src = "tsconfig.json",
    deps = [
        "//:node_modules/@types/node",
        "//packages:tsconfig_build",
    ],
)

ng_project(
    name = "service-worker",
    srcs = glob(
        [
            "*.ts",
            "src/**/*.ts",
        ],
    ),
    deps = [
        "//:node_modules/rxjs",
        "//packages/core",
    ],
)

genrule(
    name = "ngsw_config_binary",
    srcs = ["//packages/service-worker/cli:ngsw_config.js"],
    outs = ["ngsw-config.js"],
    cmd = "echo '#!/usr/bin/env node' > $@ && cat $< >> $@",
)

genrule(
    name = "ngsw_worker_renamed",
    srcs = ["//packages/service-worker/worker:ngsw_worker.js"],
    outs = ["ngsw-worker.js"],
    # Remove sourcemap since this file will be served in production site
    # See https://github.com/angular/angular/issues/23596
    cmd = "grep -v '//# sourceMappingURL' < $< > $@",
)

ng_package(
    srcs = [
        "package.json",
        "safety-worker.js",
        ":ngsw_config_binary",
        ":ngsw_worker_renamed",
        "//packages/service-worker/config:schema.json",
    ],
    package = "@angular/service-worker",
    tags = [
        "release-with-framework",
    ],
    # Do not add more to this list.
    # Dependencies on the full npm_package cause long re-builds.
    visibility = [
        "//integration:__subpackages__",
        "//modules/ssr-benchmarks:__subpackages__",
    ],
    deps = [
        ":service-worker",
        "//packages/service-worker/config",
    ],
)

api_golden_test_npm_package(
    name = "service-worker_api",
    data = [
        ":npm_package",
        "//goldens:public-api",
    ],
    golden_dir = "goldens/public-api/service-worker",
    npm_package = "packages/service-worker/npm_package",
)

filegroup(
    name = "files_for_docgen",
    srcs = glob([
        "*.ts",
        "src/**/*.ts",
    ]) + ["PACKAGE.md"],
)

api_golden_test(
    name = "service-worker_errors",
    data = [
        "//goldens:public-api",
        "//packages/service-worker",
    ],
    entry_point = "src/errors.d.ts",
    golden = "goldens/public-api/service-worker/errors.api.md",
)

generate_api_docs(
    name = "service-worker_docs",
    srcs = [
        ":files_for_docgen",
        "//packages:common_files_and_deps_for_docs",
    ],
    entry_point = ":index.ts",
    module_name = "@angular/service-worker",
)
